package copyengine.dataLayer.rpc.moudle.secret
{
	import copyengine.utils.Random;

	public class ArrayNumberSecret implements ICERpcSecretModule
	{
		private static const MAGIC_NUMBER_POSITION:int = 2;
		private static const CHECK_NUMBER_POSITION:int = 6;

		private var serverSecretKey:int;

		public function ArrayNumberSecret()
		{
		}

		public function setServerSecretKey(_key:int):void
		{
			serverSecretKey = _key;
		}

		public function encrypt(_transferLayerData:*, _requestCount:int):*
		{
			var rpcLength:int = (_transferLayerData["data"] as Array).length;
			var magicNumber:int = Random.range(0, 100);
			var checkNumber:int = rpcLength + magicNumber + serverSecretKey;
			var messArray:Array = new Array();
			for (var i:int = 0; i < 10; i++)
			{
				messArray[i] = Random.range(Math.min(magicNumber, checkNumber) - 20, Math.max(magicNumber, checkNumber));
			}
			messArray[MAGIC_NUMBER_POSITION] = magicNumber;
			messArray[CHECK_NUMBER_POSITION] = checkNumber;

			_transferLayerData["header"]["magicArray"] = messArray;
			_transferLayerData["header"]["magicNum1"] = Random.range(Math.min(magicNumber, checkNumber) - 20, Math.max(magicNumber, checkNumber));
			_transferLayerData["header"]["magicNum2"] = Random.range(Math.min(magicNumber, checkNumber) - 20, Math.max(magicNumber, checkNumber));
			_transferLayerData["header"]["magicNum3"] = Random.range(Math.min(magicNumber, checkNumber) - 20, Math.max(magicNumber, checkNumber));
			_transferLayerData["header"]["magicNum4"] = Random.range(Math.min(magicNumber, checkNumber) - 20, Math.max(magicNumber, checkNumber));
			_transferLayerData["header"]["magicNum5"] = Random.range(Math.min(magicNumber, checkNumber) - 20, Math.max(magicNumber, checkNumber));
			_transferLayerData["header"]["magicNum6"] = Random.range(Math.min(magicNumber, checkNumber) - 20, Math.max(magicNumber, checkNumber));

			return _transferLayerData;
		}

		public function decrypt(_transferLayerData:*):*
		{
			return _transferLayerData;
		}
	}
}